package com.fashion.service;

import com.fashion.dao.InLineWordDao;
import com.fashion.model.InLineWord;
import com.fashion.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: gnyk
 * Date: 12-12-30
 * Time: 下午8:31
 */
@Service("inLineWordService")
public class InLineWordServiceImpl implements InLineWordService{
    private InLineWordDao inLineWordDao;

    @Override
    public void saveOrUpdate(Object object) {
        inLineWordDao.saveOrUpdate(object);
    }

    @Override
    public void deleteEntityById(String id) {
        inLineWordDao.deleteById(id,InLineWord.class);
    }

    @Override
    public void deleteEntity(Object object) {
        inLineWordDao.delete(object);
    }

    @Override
    public List<InLineWord> searchInLineWords(String name, int pageNumber, int pageSize) {
        StringBuilder hql = new StringBuilder( "from InLineWord i ");
        Map<String, Object> parameters = new HashMap<String, Object>();
        if(!StringUtils.isNullOrEmpty(name)) {
            hql.append(" where i.word like :name ");
            parameters.put("name", "%" + name + "%");
        }

        hql.append(" order by i.createDate desc");

        return inLineWordDao.findItemsWithPage(hql.toString(), parameters, pageNumber,pageSize);
    }

    @Override
    public int countInLineWords(String name) {
        StringBuilder hql = new StringBuilder( "select count(i.id) from InLineWord i ");
        Map<String, Object> parameters = new HashMap<String, Object>();
        if(!StringUtils.isNullOrEmpty(name)) {
            hql.append(" where i.word like :name ");
            parameters.put("name", "%" + name + "%");
        }

        return inLineWordDao.countItems(hql.toString(), parameters);
    }

    @Override
    public List<InLineWord> loadAllWords() {
        return  inLineWordDao.find("from InLineWord");
    }

    @Resource(name = "inLineWordDao")
    public void setInLineWordDao(InLineWordDao inLineWordDao) {
        this.inLineWordDao = inLineWordDao;
    }
}
